//
//  MZReadingDetailController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/18.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "MZReadingDetailController.h"
#import "MZReadingDetailView.h"
#import "MZNewestViewController.h"
#import "UIView+Extention.h"
#import "leftimageButton.h"
#import "MZHottestViewController.h"
@interface MZReadingDetailController ()<UIScrollViewDelegate>
@property (nonatomic, strong)MZReadingDetailView *rd;
@property (nonatomic, strong)NSMutableArray *topArr;
@property (nonatomic, strong)UIView *underView;
@property (nonatomic, strong)UIButton *currentBtn;
@end

@implementation MZReadingDetailController

- (void)loadView
{
    self.rd = [[MZReadingDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = self.rd;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topArr = @[@"最新", @"最热"].mutableCopy;
    leftimageButton *leftButton = [leftimageButton buttonWithType:UIButtonTypeSystem];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [leftButton setTitle:self.typeName forState:UIControlStateNormal];
    [leftButton.imageView setFrame:CGRectMake(10, 7, 30, 30)];
    [leftButton setFrame:CGRectMake(10, 7, 100, 30)];
    [leftButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:leftButton];
    [self setupView];
    
}
- (void)backAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)setupView
{
    _currentBtn = nil;
    for (int i = 0;i < self.topArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(i * CGRectGetWidth(self.view.frame) / self.topArr.count,64,CGRectGetWidth(self.view.frame) / self.topArr.count, 39);
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        btn.tintColor = [UIColor clearColor];
        btn.tag = i + 2000;
        if (btn.tag == 2000) {
            _currentBtn = btn;
            _currentBtn.selected = YES;
        }
        [btn setTitle:self.topArr[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
        [self.rd addSubview:btn];
        
    }
    self.underView = [[UIView alloc] initWithFrame:CGRectMake(0, 103, CGRectGetWidth(self.view.frame) / 2, 1)];
    self.underView.backgroundColor = [UIColor blueColor];
    [self.rd addSubview:self.underView];
    MZNewestViewController *newestVC = [[MZNewestViewController alloc] initWithStyle:UITableViewStylePlain];
    newestVC.type = self.typeID;
    [self addChildViewController:newestVC];
    [self.rd.tableScr addSubview:newestVC.tableView];
    newestVC.view.y = 0;
    newestVC.view.hight = newestVC.view.hight - 84;
    
    MZHottestViewController *hottestVC = [[MZHottestViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addChildViewController:hottestVC];
    [self.rd.tableScr addSubview:hottestVC.tableView];
    hottestVC.type = self.typeID;
    hottestVC.view.x = self.rd.tableScr.size.width;
    hottestVC.view.y = 0;
    hottestVC.view.hight = hottestVC.view.hight - 84;
    self.rd.tableScr.contentSize = CGSizeMake(self.topArr.count * CGRectGetWidth(self.view.frame), 0);
    self.rd.tableScr.pagingEnabled = YES;
    self.rd.tableScr.delegate = self;
}

- (void)changeView:(UIButton *)sender
{
    [self.rd.tableScr setContentOffset:CGPointMake((sender.tag -2000) *self.rd.tableScr.size.width, 0)];
    if (_currentBtn == sender) {
        sender.selected = YES;
    } else if (_currentBtn != sender) {
        _currentBtn.selected = NO;
        sender.selected = YES;
        _currentBtn = sender;
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.underView.transform = CGAffineTransformMakeTranslation((sender.tag -2000) * self.underView.size.width, 0);
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger a = self.rd.tableScr.contentOffset.x / self.rd.tableScr.size.width;
    UIButton *btn = [self.view viewWithTag:a + 2000];
    if (_currentBtn == btn) {
        btn.selected = YES;
    } else if (_currentBtn != btn) {
        _currentBtn.selected = NO;
        btn.selected = YES;
        _currentBtn = btn;
    }

    [UIView animateWithDuration:0.1 animations:^{
        self.underView.transform = CGAffineTransformMakeTranslation(a * self.underView.size.width, 0);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
